﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Nature.Data.GMixMarkup
{
    public partial class GMixComponent : Nature.Toolkit.GMix.IComponent
    {
        ContractValuesSet m_contractValuesSet;
        Dictionary<Nature.Toolkit.GMix.ILhs, Nature.Toolkit.GMix.IRhs>
            m_equations;



        class ContractValuesSet
        {
            public string ActualID { get; set; }

            public bool IsRoot { get; set; }

            public Toolkit.GMix.UnitsSet ActualUnits { get; set; }
        }


        ContractValuesSet ContractValues
        {
            get
            {
                if (m_contractValuesSet == null)
                {
                    string id = string.IsNullOrWhiteSpace(this.ComponentID)
                        ? string.Format("*{0}", Guid.NewGuid())
                        : this.ComponentID.Trim();
                    m_contractValuesSet = new ContractValuesSet
                    {
                        ActualID = Regex.Replace(id, @"^\s*[*]\s*", string.Empty),
                        IsRoot = id.StartsWith("*"),
                        ActualUnits = new Toolkit.GMix.UnitsSet
                        {
                            CompositionScale = 1.0,
                            IsMassBased = false,
                            PressureUnits = PressureUnits.Atmosphere,
                            TemperatureUnits = TemperatureUnits.Kelvin
                        }
                    };
                    if (Units != null)
                    {

                        FractionType fractionType = this.Units.FractionType.HasValue
                            ? this.Units.FractionType.Value : FractionType.X;
                        m_contractValuesSet.ActualUnits = new Toolkit.GMix.UnitsSet
                        {
                            CompositionScale = (fractionType == FractionType.X || fractionType == FractionType.Y ? 1.0d : 100.0d),
                            IsMassBased = (fractionType == FractionType.Y || fractionType == FractionType.Y100),
                            PressureUnits = this.Units.PressureUnits.HasValue
                                ? (Nature.PressureUnits)Enum.Parse(typeof(Nature.PressureUnits), this.Units.PressureUnits.Value.ToString())
                                : PressureUnits.Atmosphere,
                            TemperatureUnits = this.Units.TemperatureUnits.HasValue
                                ? (Nature.TemperatureUnits)Enum.Parse(typeof(Nature.TemperatureUnits), this.Units.TemperatureUnits.Value.ToString())
                                : TemperatureUnits.Kelvin
                        };
                    }
                }
                return m_contractValuesSet;
            }
        }




        #region IComponent Members

        Toolkit.GMix.UnitsSet Toolkit.GMix.IComponent.ActualUnits
        {
            get { return ContractValues.ActualUnits; }
        }

        string Toolkit.GMix.IComponent.ComponentName
        {
            get
            {
                return ContractValues.ActualID;
            }
        }

        string Toolkit.GMix.IComponent.DisplayName
        {
            get
            {
                return ContractValues.ActualID;
            }
        }

        bool Toolkit.GMix.IComponent.IsRoot
        {
            get
            {
                return ContractValues.IsRoot;
            }
        }

        IEnumerable<Toolkit.GMix.ILhs> Toolkit.GMix.IComponent.LhsSet
        {
            get
            {
                if (m_equations == null)
                {
                    m_equations = this.Items.ToDictionary(i => i.Lhs, i => i.Rhs);
                }
                return m_equations.Keys;
            }
        }

        Toolkit.GMix.IRhs Toolkit.GMix.IComponent.this[Toolkit.GMix.ILhs lhs]
        {
            get { return m_equations[lhs]; }
        }

        #endregion
    }
}
